"""
行程规划数据模型
"""
from django.db import models
from django.contrib.auth import get_user_model
from apps.attractions.models import Attraction

User = get_user_model()


class Itinerary(models.Model):
    """行程规划"""
    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='itineraries',
        verbose_name='用户'
    )
    title = models.CharField(max_length=200, verbose_name='行程标题')
    start_date = models.DateField(verbose_name='开始日期')
    end_date = models.DateField(verbose_name='结束日期')
    total_budget = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        verbose_name='总预算'
    )
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'itineraries'
        verbose_name = '行程规划'
        verbose_name_plural = '行程规划'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.username} - {self.title}"


class DayPlan(models.Model):
    """每日计划"""
    itinerary = models.ForeignKey(
        Itinerary,
        on_delete=models.CASCADE,
        related_name='days',
        verbose_name='行程'
    )
    date = models.DateField(verbose_name='日期')
    day_number = models.IntegerField(verbose_name='第几天')

    class Meta:
        db_table = 'day_plans'
        verbose_name = '每日计划'
        verbose_name_plural = '每日计划'
        ordering = ['day_number']
        unique_together = ('itinerary', 'day_number')

    def __str__(self):
        return f"{self.itinerary.title} - 第{self.day_number}天"


class Activity(models.Model):
    """活动安排"""
    TRANSPORT_MODES = [
        ('walking', '步行'),
        ('driving', '自驾'),
        ('transit', '公共交通'),
        ('taxi', '出租车'),
    ]
    
    day_plan = models.ForeignKey(
        DayPlan,
        on_delete=models.CASCADE,
        related_name='activities',
        verbose_name='每日计划'
    )
    attraction = models.ForeignKey(
        Attraction,
        on_delete=models.CASCADE,
        verbose_name='景点'
    )
    start_time = models.TimeField(verbose_name='开始时间')
    end_time = models.TimeField(verbose_name='结束时间')
    transport_mode = models.CharField(
        max_length=20,
        choices=TRANSPORT_MODES,
        default='walking',
        verbose_name='交通方式'
    )
    notes = models.TextField(verbose_name='备注', blank=True)
    order = models.IntegerField(verbose_name='顺序', default=0)

    class Meta:
        db_table = 'activities'
        verbose_name = '活动安排'
        verbose_name_plural = '活动安排'
        ordering = ['order', 'start_time']

    def __str__(self):
        return f"{self.day_plan.itinerary.title} - {self.attraction.name}"


class ItineraryReminder(models.Model):
    """行程提醒"""
    REMINDER_TYPES = [
        ('start', '行程开始提醒'),
        ('day', '每日提醒'),
        ('activity', '活动提醒'),
    ]
    
    STATUS_CHOICES = [
        ('pending', '待发送'),
        ('sent', '已发送'),
        ('cancelled', '已取消'),
    ]
    
    itinerary = models.ForeignKey(
        Itinerary,
        on_delete=models.CASCADE,
        related_name='reminders',
        verbose_name='行程'
    )
    reminder_type = models.CharField(
        max_length=20,
        choices=REMINDER_TYPES,
        default='day',
        verbose_name='提醒类型'
    )
    reminder_time = models.DateTimeField(verbose_name='提醒时间')
    message = models.TextField(verbose_name='提醒内容', blank=True)
    status = models.CharField(
        max_length=20,
        choices=STATUS_CHOICES,
        default='pending',
        verbose_name='状态'
    )
    is_enabled = models.BooleanField(default=True, verbose_name='是否启用')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    sent_at = models.DateTimeField(null=True, blank=True, verbose_name='发送时间')
    
    class Meta:
        db_table = 'itinerary_reminders'
        verbose_name = '行程提醒'
        verbose_name_plural = '行程提醒'
        ordering = ['reminder_time']
        indexes = [
            models.Index(fields=['reminder_time', 'status']),
            models.Index(fields=['itinerary', 'is_enabled']),
        ]
    
    def __str__(self):
        return f"{self.itinerary.title} - {self.get_reminder_type_display()}"


